var url="";
var model;
var request;
var Hao;
var result=false;

function LoadData(){
  result=false;
  LoadPieData(true,true);
}

function LoadPieData(flag,flag2){
    request.post('http://'+url+'/app/EnergyAnalysis!ajaxEnergyAnalysisPie?projectId='+GetQueryString('projectId')+'&timeRangeType='+model.type1).success(function(data){

        if(data!=null)
        {

          var  option = {
          calculable : false,
          series : [
              {
                  name:'访问来源',
                  type:'pie',
                  radius : '75%',
                  center: ['50%', '50%'],
                  itemStyle :　{
                  normal : {//默认样式
                  color: function (value){
                   if(value.dataIndex==0) return "#21c064"
                  else if(value.dataIndex==1)  return "#e0483d";
                  else if(value.dataIndex==2)  return "#7498e6";
                  else if(value.dataIndex==3)  return "#f8931d";
                    },

                  label : {
                  show : false
                  },
                  labelLine : {
                  show : false,
                  length:-15
                  }
                  }
                  },
                  data:[
                  ]
              }
          ]
      };
     console.log(data);
      for(var i=0;i<data.length;i++)
      {
        var obj = new Object();
         obj.value = data[i].value;
         obj.name = data[i].value;
         option.series[0].data.push(obj);
      }
      //转化成百分比显示
     option.series[0].data = model.calutepercent(option.series[0].data,data);
     model.list = option.series[0].data;
     LoadPie(option);
        }
     if(flag) LoadBarData(flag2);
         }).error(function(data,status,headers,config){
      if((status>=200&&status<300)||status===304||status===1223||status===0){
      $("body").html("网络访问出错！");
      }
      });
}

function LoadBarData(flag){
      request.post('http://'+url+'/app/EnergyAnalysis!ajaxEnergyAnalysisBar?projectId='+GetQueryString('projectId')+'&energyAnalysisType='+model.type2).success(function(data){
          console.log(data);

option1 = {
    backgroundColor:'white',
    grid: {
                            x: 33,
                            y: 23,
                            x2:33,
                            y2:33,
                            width:'85%'
                        },
    calculable : false,
    xAxis : [
        {
            type : 'category',
            data : data.xAxisData
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name:'冷水机',
            type:'bar',
            stack:1,
            data:data.chiller_energy_cost
        },
       {
            name:'冷却机',
            type:'bar',
            stack:1,
            data:data.cooling_pump_energy_cost
        },
        {
            name:'冷冻机',
            type:'bar',
            stack:1,
            data:data.chilled_water_pump_energy_cost
        },
        {
            name:'冷却塔',
            type:'bar',
            stack:1,
            data:data.cooling_tower_energy_cost
        }
    ]
}; 

          Loadbar();
         
          if(flag) LoadBar2Data();
        
         }).error(function(data,status,headers,config){
      if((status>=200&&status<300)||status===304||status===1223||status===0){
      $("body").html("网络访问出错！");
      }
      });
}


function LoadBar2Data(){
      request.post('http://'+url+'/app/EnergyAnalysis!ajaxEnergyContrast.action?timeRangeType='+model.type3+'&projectId='+GetQueryString('projectId')).success(function(data){
          console.log(data);

option2 = {
    backgroundColor:'white',
    grid: {
                            x: 63,
                            y: 23,
                            x2:33,
                            y2:33,
                            width:'75%'
                        },

    legend: {
        data:['去年','今年']
    },
    calculable : false,
    xAxis : [
        {
            type : 'category',
            data : data.xAxisData,
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name:'去年',
            type:'bar',
            data:data.lastYearEnergyCostList,
        },
        {
            name:'今年',
            type:'bar',
            data:data.thisYearEnergyCostList,
        }
    ]
};

           Loadbar2();
           result=true;
         }).error(function(data,status,headers,config){
      if((status>=200&&status<300)||status===304||status===1223||status===0){
      $("body").html("网络访问出错！");
      }
      });
}

(function(){
	var app=angular.module('Hao',[]);
	app.controller('HaoController',['$http','$window',function($http,$window){
     Hao=this;
     url ="120.26.110.125/jncloud";
		request=$http;
    model=Hao;
    bindEvent();
    //Hao.barlist=new Array();
    Hao.type1 = 0;
    Hao.type2 = 0;
    Hao.type3 = 1;

   
     LoadPieData(true,true);
    Hao.calutepercent=function(list,list2){
      if(list!=null)
      {
        sum=0;
        for(var i=0;i<list.length;i++)
        {
           sum+=parseFloat(list[i].value);
        }
        for(var i=0;i<list.length;i++)
        {

          list[i].name = list2[i].name+"  "+ ((list[i].value/sum*100).toFixed(2)+"%");
        }
      }
      return list;
    }

    Hao.changetype1=function(type){
       Hao.type1=type;
       LoadPieData(false,false);
    }
    Hao.changetype2=function(type){
       Hao.type2=type;
        LoadBarData(false);
    }
    Hao.changetype3=function(type){
       Hao.type3=type;
       LoadBar2Data();
    }
	}]);
})();
